package a51qindian.com.comprehensive.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.li.widget.MultipleLayout;
import com.li.widget.TopNavigationBar;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.adapter.CouponAdapter;
import a51qindian.com.comprehensive.base.QDBaseActivity;
import a51qindian.com.comprehensive.items.LinearItemDecoration;
import a51qindian.com.comprehensive.model.BaseModel;
import a51qindian.com.comprehensive.model.CouponInfo;
import a51qindian.com.comprehensive.net.ObservableHelper;
import a51qindian.com.comprehensive.net.RetrofitCreator;
import a51qindian.com.comprehensive.net.SelectorObserver;
import butterknife.BindView;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/10/16 0016
 * Description: 已验证优惠券列表
 */

public class CouponListActivity extends QDBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.recycler_coupon)
    RecyclerView mRecyclerCoupon;
    @BindView(R.id.layout_content)
    MultipleLayout mLayoutContent;

    private CouponAdapter mAdapter;

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);
        mRecyclerCoupon.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerCoupon.addItemDecoration(new LinearItemDecoration());
        mAdapter = new CouponAdapter(this);
        mRecyclerCoupon.setAdapter(mAdapter);
        getCouponList(mUid);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_coupon_list;
    }

    /**
     * 获取优惠券列表
     *
     * @param uid 用户编号
     */
    private void getCouponList(String uid) {
        mLayoutContent.onLoadingWithContent();
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        ObservableHelper.createObservable(RetrofitCreator.createService().couponList(map))
                .subscribe(new SelectorObserver<BaseModel<List<CouponInfo>>>() {
                    @Override
                    protected void onErrorCodeInfo(int code, String info) {
                        mLayoutContent.onSuccess();
                        showToast(info);
                    }

                    @Override
                    protected void onSuccess(BaseModel<List<CouponInfo>> listBaseModel) {
                        mLayoutContent.onSuccess();
                        List<CouponInfo> couponInfoList = listBaseModel.getData();
                        mAdapter.setCouponInfoList(couponInfoList);
                    }

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mLayoutContent.onSuccess();
                        showToast(e.getMessage());
                    }
                });
    }
}
